class Solution {
public:
    int mySqrt(int x) {
        if (x == 0)
            return 0;
        int left = 1;
        int right = x / 2;
        while (right > left)
        {
            int min = left + (right - left + 1) / 2;
            if (min > x / min)
            {
                right = min - 1;
            }
            else if (min < x / min)
            {
                left = min;
            }
            else if (min == x / min)
            {
                return min;
            }
        }
        return left;
    }
};